import config from "../config"
import store from "../utils/factory"
import {
  lostLogin
} from "../utils"
/**
 * 统一调用接口
 */
function request(url, data, method) {
  return new Promise((resolve, reject) => {
    mpvue.showLoading({
      title: "加载中..."
    });
    mpvue.request({
      url: config.baseUrl + url,
      data: data || {},
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'cookie': store.get("cookie") || ""
      },
      method: method || 'GET',
      dataType: 'json',
      responseType: 'text',
      success: res => {
        mpvue.hideLoading()
        let statusCode = res.statusCode
        if (res.header["Set-Cookie"]) {
          let header = res.header;
          store.set("cookie", header["Set-Cookie"])
        }
        if (statusCode == 200) {
          let req = res.data;
          if (req.code == 0 || !req.code) {
            resolve(req)
          } else {
            if (req.code == 500) {
              store.remove("loginInfo");
              mpvue.showModal({
                title: "提示",
                content: "登录已过期，请重新登录",
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    mpvue.navigateTo({
                      url: "../login/main"
                    });
                  }
                }
              });
              return false;
            }
            mpvue.showToast({
              title: req.msg || "服务器异常",
              icon: 'none',
              duration: 2000
            })
            reject(req)
          }
        } else {
          if (statusCode >= 500) {
            mpvue.showToast({
              title: '服务器异常，请稍后再试',
              icon: 'none',
              duration: 2000
            })
            reject(res)
          } else {
            if (statusCode == 401) {
              lostLogin();
            } else {
              mpvue.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        }
      },
      fail: res => {
        mpvue.hideLoading()
        mpvue.showToast({
          title: '网络异常，请检查网络状态',
          icon: 'none',
          duration: 2000
        })
        reject(res)
      },
    })
  })
}


/**
 * 
 * 登录
 * @method login
 * @param {username,password}
 * 
 */
const login = function (data) {
  let url = 'user/login'
  return request(url, data, "POST")
}

/**
 * 
 * 退出登录
 * @method login
 * @param {username,password}
 * 
 */
const logout = function (data) {
  let url = 'user/logout'
  return request(url, data, "POST")
}

/**
 * 
 * 获取配送列表
 * @method getMsgList
 * @param {formdata}
 * 
 */
const getMsgList = function (data) {
  let url = 'query/delivery'
  return request(url, data, "GET")
}

/**
 * 
 * 获取未配送列表
 * @method getMsgList
 * @param {formdata}
 * 
 */
const getUndoList = function (data) {
  let url = 'query/undoDelivery'
  return request(url, data, "GET")
}

/**
 * 
 * 获取客户列表
 * @method getCustomerList
 * @param {formdata}
 * 
 */
const getCustomerList = function (data) {
  let url = 'customer/query'
  return request(url, data, "POST")
}


/**
 * 
 * 保存客户
 * @method saveCustomer
 * @param {formdata}
 * 
 */
const saveCustomer = function (data) {
  let url = 'customer/update'
  return request(url, data, "POST")
}

/**
 * 
 * 补单
 * @method supplementSheet
 * @param {formdata}
 * 
 */
const supplementSheet = function (data) {
  let url = 'delivery/supplement'
  return request(url, data, "POST")
}


/**
 * 
 * 获取总人数
 * @method userQuery
 * @param {formdata}
 * 
 */
const userQuery = function (data) {
  let url = 'user/query'
  return request(url, data, "POST")
}

/**
 * 
 * 切换账号
 * @method changeArea
 * @param {formdata}
 * 
 */
const changeArea = function (data) {
  let url = '/switch/' + data;
  return request(url, null, "POST")
}

const queryProject = function (data) {
  let url = 'project/query';
  return request(url, null, "POST")
}

/**
 * 
 * 获取配送详情
 * @method getMsgDetail
 * 
 */
const getMsgDetail = function (data) {
  let url = `delivery/getById`
  return request(url, data, "POST")
}

/**
 * 
 * 获取跟类目
 * @method getRoot
 * 
 */
const getRoot = function (data) {
  let url = `category/getRoot`
  return request(url, data, "POST")
}

/**
 * 
 * 获取子类目
 * @method getSub
 * 
 */
const getSub = function (data) {
  let url = `category/getSub`
  return request(url, data, "POST")
}

/**
 * 
 * 确认配送
 * @method confirmDelivery
 * 
 */
const confirmDelivery = function (data) {
  let url = 'delivery/confirm';
  return request(url, data, "POST")
}

/**
 * 
 * 审核配送
 * @method startDelivery
 * 
 */
const startDelivery = function (data) {
  let url = 'delivery/pass';
  return request(url, data, "POST")
}

/**
 * 
 * 开始配送
 * @method letsGoDelivery
 * 
 */
const letsGoDelivery = function (data) {
  let url = 'delivery/start';
  return request(url, data, "POST")
}

/**
 * 
 * 签约客户
 * @method doneCustomer
 * 
 */
const doneCustomer = function (data) {
  let url = 'customer/done';
  return request(url, data, "POST")
}

/**
 * 
 * 签约客户
 * @method releaseCustomer
 * 
 */
const releaseCustomer = function (data) {
  let url = 'customer/release';
  return request(url, data, "POST")
}

/**
 * 
 * 查看小类
 * @method getDetail
 * 
 */
const getDetail = function (data) {
  let url = 'store/getByCategoryId';
  return request(url, data, "POST")
}

/**
 * 
 * 新增配送
 * @method addDelivery
 * 
 */
const addDelivery = function (data) {
  let url = 'delivery/add';
  return request(url, data, "POST")
}

/**
 * 
 * 批量配送
 * @method batch
 * 
 */
const batchAdd = function (data) {
  let url = 'delivery/batchAdd';
  return request(url, data, "POST")
}

// GET /message/{id}

export {
  getCustomerList,
  getMsgList,
  login,
  logout,
  batchAdd,
  supplementSheet,
  confirmDelivery,
  startDelivery,
  addDelivery,
  userQuery,
  queryProject,
  getMsgDetail,
  getUserDetail,
  saveCustomer,
  distribute,
  deleteCustomer,
  doneCustomer,
  releaseCustomer,
  addCustomer,
  redundant,
  delMsg,
  getSub,
  getRoot,
  getDetail,
  getUndoList,
  letsGoDelivery
}
